package cn.lecosa.util

import org.apache.commons.lang3.StringUtils
import scalikejdbc.{DB, _}

import scala.collection.mutable

/**
  * s_nrbzhys_dm  表服务
  */
object DmService {

  DBUtils.getDbConnection()

  private var map = mutable.HashMap.empty[String, String]
  private val bzhdmMap = new mutable.HashMap[String, String]
  private val bzhmcMap = new mutable.HashMap[String, String]
  private val dmMap = new mutable.HashMap[String, mutable.HashMap[String, String]]()

  /**
    * 初始化标准化代码
    *
    * @param ysbm
    */
  private def initBzhdm(ysbm: String) = {

    val tuples = DB.readOnly { implicit session => {
      sql"SELECT t.ysmb,t.ysdm, t.ysmc, t.bzhdm from s_nrbzhys_dm t where ysmb = ?"
        .bind(ysbm)
        .map(rs => {
          (StringUtils.trimToEmpty(rs.string("ysmb")),
            StringUtils.trimToEmpty(rs.string("ysdm")),
            StringUtils.trimToEmpty(rs.string("ysmc")),
            StringUtils.trimToEmpty(rs.string("bzhdm")))
        }).list().apply()
    }
    }

    tuples.foreach(x => {
      bzhdmMap(x._1 + "_" + x._2) = x._4
      bzhdmMap(x._1 + "_" + x._3) = x._4
    })

  }

  /**
    * 初始化标准化名称
    *
    * @param ysbm
    */
  private def initbzhmc(ysbm: String) = {
    val lists = DB.readOnly { implicit session => {
      sql"SELECT t.ysmb,t.ysdm, t.ysmc, t.bzhmc from s_nrbzhys_dm t where ysmb = ?"
        .bind(ysbm)
        .map(rs => {
          (StringUtils.trimToEmpty(rs.string("ysmb")),
            StringUtils.trimToEmpty(rs.string("ysdm")),
            StringUtils.trimToEmpty(rs.string("ysmc")),
            StringUtils.trimToEmpty(rs.string("bzhmc")))
        }).list().apply()
    }
    }

    lists.foreach(x => {
      bzhmcMap(x._1 + "_" + x._2) = x._4
      bzhmcMap(x._1 + "_" + x._3) = x._4
    })

  }

  /**
    * 根据分隔符获取标准化名称
    *
    * @param ysbm
    * @param valueStr
    * @param separator
    */
  def getBzhmcBySp(ysbm: String, valueStr: String, separator: String) = {


    if (bzhmcMap.isEmpty) {
      this.synchronized {
        initbzhmc(ysbm)
      }
    }


    val sb = new mutable.StringBuilder()
    var status = 0;
    valueStr.split(separator).foreach(x => {
      val str = StringUtils.trimToEmpty(x)
      val maybeString = bzhmcMap.get(ysbm + "_" + str)
      if (maybeString.nonEmpty) {
        sb.append(str).append(separator)
      } else {
        sb.append(str).append(separator)
        status = 1000;
      }
    })
    sb.delete(sb.lastIndexOf(separator), sb.length)
    (Option(sb.toString()), status)
  }

  /**
    * 根据分隔符获取标准化名称
    *
    * @param ysbm
    * @param valueStr
    * @param separator
    */
  def getBzhdmBySp(ysbm: String, valueStr: String, separator: String): (Option[String], Int) = {


    if (bzhdmMap.isEmpty) {
      this.synchronized {
        initBzhdm(ysbm)
      }
    }
    var status = 0;
    val sb = new mutable.StringBuilder()
    valueStr.split(separator).foreach(x => {
      val ysdm = StringUtils.trimToEmpty(x)
      val str = bzhdmMap.get(ysbm + "_" + ysdm)
      if (str.nonEmpty) {
        sb.append(str).append(separator)
      } else {
        sb.append(ysdm).append(separator)
        status = 1000;
      }
    })
    sb.delete(sb.lastIndexOf(separator), sb.length)
    (Option(sb.toString()), status)
  }


  /**
    * 初始化码表
    * 获取代码
    *
    * @return
    */
  @Deprecated
  def getalldm = {
    val tuples = DB.readOnly { implicit session => {
      sql"SELECT t.bzhdmbm,t.ysdm, t.ysmc, t.bzhdm from s_nrbzhys_dm t"
        .map(rs => {
          (StringUtils.trimToEmpty(rs.string("bzhdmbm")),
            StringUtils.trimToEmpty(rs.string("ysdm")),
            StringUtils.trimToEmpty(rs.string("ysmc")),
            StringUtils.trimToEmpty(rs.string("bzhdm")))
        }).list().apply()
    }
    }
    tuples.foreach(x => {
      map(x._1 + "_" + x._2) = x._4
      map(x._1 + "_" + x._3) = x._4
    })
    map.toMap
  }

  /**
    * 初始化码表
    * 获取value
    *
    * @return
    */
  @Deprecated
  def getalldmvalue = {
    val tuples = DB.readOnly { implicit session => {
      sql"SELECT t.bzhdmbm,t.ysdm, t.ysmc, t.bzhmc from s_nrbzhys_dm t"
        .map(rs => {
          (StringUtils.trimToEmpty(rs.string("bzhdmbm")),
            StringUtils.trimToEmpty(rs.string("ysdm")),
            StringUtils.trimToEmpty(rs.string("ysmc")),
            StringUtils.trimToEmpty(rs.string("bzhmc")))
        }).list().apply()
    }
    }
    tuples.foreach(x => {
      map(x._1 + "_" + x._2) = x._4
      map(x._1 + "_" + x._3) = x._4
    })
    map.toMap
  }

  /**
    * 根据原始码表名获取所有映射码值
    *
    * @param ysmb
    * @return
    */
  
  
  
  def getBzhdmByYsmb(ysmb: String) = {
    if (!dmMap.contains(ysmb)) {
      val tuples = DB.readOnly { implicit session => {
        sql"SELECT t.id,t.t1, t.id2 from test t where id = ?"  .bind(ysmb).map(rs => {
            (StringUtils.trimToEmpty(rs.string("id")),
              StringUtils.trimToEmpty(rs.string("t1")),
              StringUtils.trimToEmpty(rs.string("id2")))
          }).list().apply()
      }
      }

      
      val map = new mutable.HashMap[String, String]
      tuples.foreach(r => {
        val value = replaceNull(r._1) + "_" + replaceNull(r._2)
        map.+=(r._2 -> value)
        map.+=(r._3 -> value)
      })
      dmMap.+=(ysmb -> map)
      //     println("原始码表: "+ysmb +"原始代码  "  + map)
    }
    dmMap.toMap
  }

  def replaceNull(str: String) = {
    if (str.isEmpty) (
      " "
      ) else {
      str
    }
  }
}
